$(function() {
    // 全局变量加锁法
    var loginBlock = true;
    $('#login-btn').on('click', function() {
        console.log(loginBlock);

        var username = $('[name="username"]').val();
        var password = $('[name="password"]').val();
        $.ajax({
            type: 'post',
            url: '/user/login',
            data: {
                username: username,
                password: password,
            },
            beforeSend: function() {
                if (loginBlock) {
                    loginBlock = false;
                } else {
                    return false;
                };
            },
            success: function(res) {
                console.log(res);
                if (res.success) {
                    mui.toast('登录成功');
                    $('#login-btn').html('正在登录...');
                    setTimeout(function() {
                        location.href = 'user.html';
                    }, 2000);
                } else {
                    mui.toast(res.message);
                    $('#login-btn').html('请重新登录');
                }
            },
            error: function() {
                loginBlock = true;
            },

        })
    });

});